﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MX.Core.Http.WebAction
{
    /// <summary>
    /// WEB 请求类
    /// </summary>
    static public class WebHost
    {
        /// <summary>
        /// 一个类的实例为一个cookie容器
        /// </summary>
        class WebBrowser : WebPageAction
        {
            /// <summary>
            /// form表单请求
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="url"></param>
            /// <param name="postData"></param>
            /// <returns></returns>
            public T Post<T>(string url, object postData)
            {
                try
                {
                    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(postData));
                    var nvc = new NameValueCollection();
                    foreach (var key in dict.Keys)
                    {
                        nvc[key] = dict[key];
                    }
                    XWwwFormUrlencodedPostInfo postInfo = new XWwwFormUrlencodedPostInfo(nvc);
                    NameValueCollection headers = new NameValueCollection();
                    headers["X-Requested-With"] = "XMLHttpRequest";
                    string json = GetWebData<string>(url, postInfo: postInfo, headers: headers);
                    return JsonConvert.DeserializeObject<T>(json);
                }
                catch (Exception ex)
                {
                    return default(T);
                }
            }

            /// <summary>
            /// json表单请求
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="url"></param>
            /// <param name="postData"></param>
            /// <returns></returns>
            public T PostByJson<T>(string url, object postData)
            {
                try
                {
                    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(postData));
                    var nvc = new NameValueCollection();
                    foreach (var key in dict.Keys)
                    {
                        nvc[key] = dict[key];
                    }
                    JsonPostInfo postInfo = new JsonPostInfo(nvc);
                    NameValueCollection headers = new NameValueCollection();
                    headers["X-Requested-With"] = "XMLHttpRequest";
                    string json = GetWebData<string>(url, postInfo: postInfo, headers: headers);
                    return JsonConvert.DeserializeObject<T>(json);
                }
                catch (Exception ex)
                {
                    return default(T);
                }
            }
        }
        static WebBrowser wb = new WebBrowser();

        /// <summary>
        /// POST form http请求
        /// </summary>
        /// <typeparam name="T">返回的数据类型</typeparam>
        /// <param name="url">请求的URL</param>
        /// <param name="postData">post 的数据</param>
        /// <returns></returns>
        static public T Post<T>(string url, object postData)
        {
            return wb.Post<T>(url, postData);
        }


        /// <summary>
        /// POST Json http请求
        /// </summary>
        /// <typeparam name="T">返回的数据类型</typeparam>
        /// <param name="url">请求的URL</param>
        /// <param name="postData">post 的数据</param>
        /// <returns></returns>
        static public T PostByJson<T>(string url, object postData)
        {
            return wb.Post<T>(url, postData);
        }
    }
}
